package com.backend_pay.controller.user;

import com.backend_pay.entity.User;
import com.backend_pay.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;

import java.sql.Timestamp;
import java.util.Objects;
import java.util.Optional;

@Controller //展示层
public class loginController {
    //自动注入服务层业务层UserService
    @Autowired
    UserService userService;
    /*登录验证*/
    @PostMapping(value="/login")
    @ResponseBody
    public String login(@RequestBody User data){
        User user=new User();
        user=userService.findByName(data.getName());

        if(Objects.isNull(user)){
            return "用户名不存在!";
        }
        if(data.getPassword().equals(user.getPassword())) {//if(Objects.equals(user.getPassword(), password)){
            return "登录成功";
        }else{
            return "密码错误！";
        }
    }
    //获取userInfo
    @GetMapping(value="/getUserInfo/{name}")
    @ResponseBody
    public User getUserInfo(@PathVariable("name") String name){
        User user;
        user=userService.findByName(name);
        user.setPassword("密码不可见");
        return user;
    }
    //获取userInfo
    @PostMapping(value="/getUserInfoByName")
    @ResponseBody
    public User getUserInfoByName(@RequestBody User data){
        User user;
        user=userService.findByName(data.getName());
        user.setPassword("密码不可见");
        return user;
    }
    //更新登录时间
    @PostMapping(value="/updateloginTime/{id}")
    @ResponseBody
    public void updateLoginTime(@PathVariable("id") Integer id){
        User user;
        user=userService.findByUserid(id);
        //更新登录时间
        Timestamp time=new Timestamp(System.currentTimeMillis());
        user.setLastlogintime(time);
        userService.save(user);
    }
}
